package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.model.dto.FavoriteDTO;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IFavoriteService;
import com.example.mybatisplusdemo.model.domain.Favorite;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-04
 * @version v1.0
 */
@RestController
@RequestMapping("/api/favorite")
public class FavoriteController {

    private final Logger logger = LoggerFactory.getLogger( FavoriteController.class );

    @Autowired
    private IFavoriteService favoriteService;

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        Favorite favorite = favoriteService.getById(id);
        return JsonResponse.success(favorite);
    }

    //用户收藏店铺
    @PostMapping("addFavoriteStore")
    public JsonResponse addFavoriteStore(@RequestBody Favorite favorite) {
        boolean b = favoriteService.save(favorite);
        return JsonResponse.success(b);
    }

    //用户取消收藏店铺
    @PostMapping("removeFavoriteStore")
    public JsonResponse removeFavoriteStore(@RequestBody Favorite favorite) {
        // 用户取消收藏店铺的逻辑在服务层实现
        boolean isRemoved = favoriteService.removeFavoriteStore(favorite);
        return JsonResponse.success(isRemoved);
    }

    //获取收藏列表（分页）
    @GetMapping("listFavoriteByUserId")
    public JsonResponse listFavoriteByUserId(PageDTO pageDTO, Long userId) {
        Page<FavoriteDTO> page = favoriteService.listFavoriteByUserId(pageDTO, userId);
        return JsonResponse.success(page);
    }

    //更新收藏标签
    @PostMapping("updateTagsByFavoriteId")
    public JsonResponse updateTagsById(@RequestBody Favorite favorite) {
        boolean b = favoriteService.updateById(favorite);
        return JsonResponse.success(b);
    }

    //判断用户是否收藏了某个店铺
    @GetMapping("getFavoriteByUserStore")
    public JsonResponse getFavoriteByUserStore(Long userId, Long storeId) {
        boolean b = favoriteService.getFavoriteByUserStore(userId, storeId);
        return JsonResponse.success(b);
    }
}

